home *** CD-ROM | disk | FTP | other *** search
- #include "SampleDefs.h"
- #include "Sample.h"
- #include "SampleErrors.h"
-
- #include <Memory.h>
- #include <Packages.h>
- #include <Fonts.h>
- #include <ToolUtils.h>
-
- #include <String.h>
-
- #define POINT_TO_LONG(aPoint) (* (long *) &(aPoint) )
-
-
- #pragma segment Main
- /***************************************************************************************
-
- LongIntToHex
-
- Converts a long into a hexadecimal string.
-
- ****************************************************************************************/
- void LongIntToHex(long decNumber, Str255 hexNumber, short noOfDigits)
- {
- short i;
- char realNumberOfDigits;
-
- if (noOfDigits > 8)
- realNumberOfDigits = 8;
- else
- realNumberOfDigits = noOfDigits;
-
- hexNumber[0] = realNumberOfDigits;
- for (i = realNumberOfDigits; i >= 1; --i) {
- hexNumber[i] = "0123456789ABCDEF"[(decNumber & 0x0F)];
- decNumber >>= 4;
- }
- }
-
- #pragma segment Main
- /***************************************************************************************
-
- DisplayErrRec
-
- This routine is called by the Dialog Manager to image our custom item. This
- custom item displays an error number, an event record, and a block of text
- explaining what occured and where.
-
- ****************************************************************************************/
- pascal void DisplayErrRec(DialogPtr dptr, short item)
- {
- #pragma unused (item) /* unused formal parameters */
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- short fnum;
- GrafPtr currentPort;
- PortFontInfo pfi;
- FontInfo fi;
- ErrorRec *er;
- Str255 numstr;
- short lheight;
- Str255 OSString = "\p 1234";
- Str255 tempString;
-
- /* get the port and save the font info */
-
- GetPort(¤tPort);
- pfi.txFont = currentPort->txFont;
- pfi.txSize = currentPort->txSize;
- pfi.txFace = currentPort->txFace;
-
- /* get the useritem rect */
-
- GetDItem(dptr, A_USERITEM, &itemType, &itemHandle, &itemRect);
-
- /* set the font information we need */
- GetFNum("\pHelvetica", &fnum);
- TextFont(fnum);
- TextSize(12);
- TextFace(normal);
-
- /* get the font information so we know the line height */
- GetFontInfo(&fi);
- lheight = fi.ascent + fi.descent + fi.leading;
-
- /* indent by insetting the rectangle */
- InsetRect(&itemRect, 2, 2);
-
- /* Get the event info */
- er = (ErrorRec *)GetWRefCon(dptr);
-
- if (er != nil) {
- /* Display message that an error occurred. */
- MoveTo(itemRect.left, itemRect.top + fi.ascent);
- GetIndString(tempString, ERR_DLOG_ID, sErrorOccurred);
- DrawString(tempString);
- itemRect.top = itemRect.top + lheight;
-
- if (er->err != 0) {
- /* draw the error number */
- MoveTo(itemRect.left, itemRect.top + fi.ascent);
- GetIndString(tempString, ERR_DLOG_ID, sErrorNumber);
- NumToString(er->err, numstr);
- DrawString(tempString); DrawString(numstr);
- itemRect.top = itemRect.top + lheight;
- }
-
- if (er->event != nil) {
- /* draw event.what */
- MoveTo(itemRect.left, itemRect.top + fi.ascent);
- GetIndString(tempString, ERR_DLOG_ID, sEventWhat);
- NumToString(er->event->what, numstr);
- DrawString(tempString); DrawString(numstr);
- itemRect.top = itemRect.top + lheight;
-
- /* draw event.message */
- MoveTo(itemRect.left, itemRect.top + fi.ascent);
- GetIndString(tempString, ERR_DLOG_ID, sEventMessage);
- LongIntToHex(er->event->message, numstr, 8);
- BlockMove((Ptr) &er->event->message, &OSString[3], 4);
- DrawString(tempString); DrawString(numstr); DrawString(OSString);
- itemRect.top = itemRect.top + lheight;
-
- /* draw event.where */
- MoveTo(itemRect.left,itemRect.top + fi.ascent);
- GetIndString(tempString, ERR_DLOG_ID, sMessageID);
- /* need to convert the where into a long */
- LongIntToHex(POINT_TO_LONG(er->event->where), numstr, 8);
- BlockMove((Ptr) &er->event->where, &OSString[3], 4);
- DrawString(tempString); DrawString(numstr); DrawString(OSString);
- itemRect.top = itemRect.top + lheight;
- }
-
- if (er->message != nil)
- TextBox(er->message, strlen(er->message), &itemRect, teFlushDefault);
- }
-
- /* restore port font information */
- TextFont(pfi.txFont);
- TextSize(pfi.txSize);
- TextFace(pfi.txFace);
-
- }
-
-
- #pragma segment Main
- /***************************************************************************************
-
- DisplayError
-
- If an error occurs this routine is called. It displays the error, the contents
- of the event record, and a block of explanatory text.
-
- ****************************************************************************************/
- void DisplayError( OSErr err, EventRecord *evt, char *message )
- {
- DialogPtr dptr;
- Handle itemHandle;
- short itemType;
- Rect itemRect;
- ErrorRec er;
- short item;
-
- dptr = GetNewDialog(ERR_DLOG_ID, nil, (WindowPtr)-1L);
-
- if (dptr) {
- GetDItem(dptr,A_USERITEM,&itemType,&itemHandle,&itemRect);
- SetDItem(dptr,A_USERITEM,itemType,(Handle)DisplayErrRec,&itemRect);
-
- er.err = err;
- er.event = evt;
- er.message = message;
-
- SetWRefCon( dptr, (long)&er );
- ShowWindow( dptr );
- do {
- ModalDialog(nil,&item);
- } while ( item != ok );
-
- DisposDialog(dptr);
- }
- }
-
-
- #pragma segment Main
- /***************************************************************************************
-
- ShowElapsedTime
-
- ****************************************************************************************/
- void ShowElapsedTime(long elapsedTime)
- {
- DialogPtr dptr;
- short item;
- Str255 tempString1;
- Str255 tempString2;
-
- dptr = GetNewDialog(ELAPSED_DLOG_ID, nil, (WindowPtr)-1L);
-
- if (dptr) {
- ShowWindow(dptr);
- GetIndString(tempString1, ELAPSED_DLOG_ID, sElapsedTime);
- NumToString(elapsedTime, tempString2);
- ParamText(tempString1, tempString2, nil, nil);
- do {
- ModalDialog(nil, &item);
- } while ( item != ok );
-
- DisposDialog(dptr);
- }
- }
-